package rox

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"utilx/module"
)

func HandleSuccess[T any](ctx *gin.Context, msg string, data T, handles ...func(rec *module.Result[T])) {
	m := module.Result[T]{
		Code: 200,
		Msg:  msg,
		Data: data,
	}
	for _, handle := range handles {
		handle(&m)
	}
	ctx.JSON(http.StatusOK, m)
	ctx.Abort() // 强制推出
}
func HandleErr[T any](ctx *gin.Context, msg string, data T, handles ...func(rec *module.Result[T])) {
	m := module.Result[T]{
		Code: 400,
		Msg:  msg,
		Data: data,
	}
	for _, handle := range handles {
		handle(&m)
	}
	ctx.JSON(http.StatusOK, m)
	ctx.Abort() // 强制推出
}
func HandleMsg[T any](ctx *gin.Context, code int, msg string, data T, handles ...func(rec *module.Result[T])) {
	m := module.Result[T]{
		Code: code,
		Msg:  msg,
		Data: data,
	}
	for _, handle := range handles {
		handle(&m)
	}
	ctx.JSON(http.StatusOK, m)
	ctx.Abort() // 强制推出
}

type IRoInitRouter interface {
	InitRouter(group *gin.RouterGroup)
}

// ... 可变参数 【】array

func InitRouter(group *gin.RouterGroup, routers ...IRoInitRouter) {
	//_ 不使用 i index
	for _, router := range routers {
		router.InitRouter(group)
	}
}

type IRouters []IRoInitRouter

//==============

func BindBoyJsonErr[T any](ctx *gin.Context, v *T) bool {
	err := ctx.ShouldBindJSON(v)
	if err != nil {
		HandleErr[string](ctx, "params err", err.Error())
		return true
	}
	fmt.Println("======================")
	fmt.Println(v)
	fmt.Println("======================")
	return false
}
